import {loginEmail, loginUser, register} from "../Axios/User.js"
import {saveState} from '../localStorages/localStorageUesrData.js'
import {showToast} from "../Toast.js"

// 登录页面渲染
//呈现登录页面
function renderLoginPage() {
    document.getElementById('login-container').style.display = 'block';
    document.getElementById('register-container').style.display = 'none';
}

// 呈现注册页面
function renderRegisterPage() {
    document.getElementById('login-container').style.display = 'none';
    document.getElementById('register-container').style.display = 'block';
}

// 验证邮箱格式
function validateEmail(email) {
    const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return re.test(String(email).toLowerCase());
}

//提交注册表格
function submitRegisterForm() {
    const password = document.getElementById('register-password').value;
    const name = document.getElementById('register-username').value;
    const confirmPassword = document.getElementById('register-confirm-password').value;
    const email = document.getElementById('register-email').value;

    if (password !== confirmPassword) {
        document.getElementById('password-error').textContent = '两次输入的密码不一致';
        return;
    } else {
        document.getElementById('password-error').textContent = '';
    }

    if (!validateEmail(email)) {
        document.getElementById('password-error').textContent = '邮箱格式不正确';
        return;
    } else {
        document.getElementById('password-error').textContent = '';
    }

    // 这里可以添加注册逻辑，比如发送数据到服务器
    // 定义要发送的数据
    const data = {
        name: name,
        password: password,
        email: email
    };
    console.log(data)

    register(data).then(data => {
        loginFormAssign(name, password); // 登录表单赋值
        renderLoginPage()
    }).catch( ex => showToast(ex.response.data.message,"error"));

}

// 登录表格赋值
function loginFormAssign(name, password) {
    document.getElementById('login-username-email').value = name;
    document.getElementById('login-password').value = password;

}
// 存储 数据 到本地 location
function dataLocation(data) {
    window.location.replace('/');

    let userData = data.data.data;
    console.log(userData)
    saveState(userData.jwtKey, userData.name, userData.email, userData.introduction, userData.image); // 保存用户信息
}

// 提交登录表格
function submitLoginForm() {
    const usernameOrEmail = document.getElementById('login-username-email').value;
    const password = document.getElementById('login-password').value;



    if (validateEmail(usernameOrEmail)) {
        // 邮箱登录
        const data = {
            password: password,
            email: usernameOrEmail
        };
        // 定义要发送的数据
        loginEmail(data).then(data => dataLocation(data))
            .catch( ex =>showToast(ex.response.data.message,"error"));
    } else {
        // 定义要发送的数据
        const data = {
            name: usernameOrEmail,
            password: password,
        };
        // 用户名登录
        loginUser(data).then(data => dataLocation(data))
            .catch( ex => showToast(ex.response.data.message,"error"));
    }
}


export {
    renderLoginPage, renderRegisterPage, validateEmail,
    submitRegisterForm, submitLoginForm
}
